package com.ruoyi.system.service.impl;

import java.util.List;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.PenJsonV2Mapper;
import com.ruoyi.system.domain.PenJsonV2;
import com.ruoyi.system.service.IPenJsonV2Service;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-11
 */
@Service
public class PenJsonV2ServiceImpl implements IPenJsonV2Service
{
    @Autowired
    private PenJsonV2Mapper penJsonV2Mapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public PenJsonV2 selectPenJsonV2ById(Long id)
    {
        return penJsonV2Mapper.selectPenJsonV2ById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param penJsonV2 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<PenJsonV2> selectPenJsonV2List(PenJsonV2 penJsonV2)
    {
        return penJsonV2Mapper.selectPenJsonV2List(penJsonV2);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param penJsonV2 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertPenJsonV2(PenJsonV2 penJsonV2)
    {
        return penJsonV2Mapper.insertPenJsonV2(penJsonV2);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param penJsonV2 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updatePenJsonV2(PenJsonV2 penJsonV2)
    {
        return penJsonV2Mapper.updatePenJsonV2(penJsonV2);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deletePenJsonV2ByIds(Long[] ids)
    {
        return penJsonV2Mapper.deletePenJsonV2ByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deletePenJsonV2ById(Long id)
    {
        return penJsonV2Mapper.deletePenJsonV2ById(id);
    }

    @Override
    public void insertpenjsonv2(String penjson, Long paperid, PenJsonV2 penJsonV2) {
        // MMSE五边形
        if(paperid==69142l) {
            int stx =940;
            int sty = 1720;
            int endx = 1980;
            int endy = 2700;
            JSONArray mmse = new JSONArray();
            JSONArray json = new JSONArray(penjson);
            for (int j = 0; j < json.size(); j++) {
                JSONObject pj = json.getJSONObject(j);
                int jx = (int) Math.round(pj.getDouble("x") * 11.819955);
                int jy = (int) Math.round(pj.getDouble("y") * 11.819955);
                if (jx >= stx && jx <= endx && jy >= sty && jy <= endy) {
                    pj.set("x", pj.getDouble("x") - stx * 1.0 / 11.81995);
                    pj.set("y", pj.getDouble("y") - sty * 1.0 / 11.81995);
                    mmse.put(pj);
                }
            }
            penJsonV2.setMMSE(mmse.toString());
            penJsonV2Mapper.updatePenJsonV2(penJsonV2);
        }else if(paperid==69148l){
            //tmt1a
            int stx =230;
            int sty = 455;
            int endx  = 1840;
            int endy = 1610;
            JSONArray tmt1a = new JSONArray();
            JSONArray json = new JSONArray(penjson);
            for (int j=0;j<json.size();j++){
                JSONObject pj = json.getJSONObject(j);
                int jx = (int) Math.round(pj.getDouble("x")*11.819955);
                int jy = (int) Math.round(pj.getDouble("y")*11.819955);
                if(jx>=stx&&jx<=endx&&jy>=sty&&jy<=endy){
                    pj.set("x",pj.getDouble("x")-stx*1.0/11.81995);
                    pj.set("y",pj.getDouble("y")-sty*1.0/11.81995);
                    tmt1a.put(pj);
                }
            }
            penJsonV2.setTMT1A(tmt1a.toString());
            penJsonV2Mapper.updatePenJsonV2(penJsonV2);
        }else if(paperid==69149l) {
            //tmt1b
            int stx = 60;
            int sty = 300;
            int endx = 2010;
            int endy = 2890;
            JSONArray tmt1b = new JSONArray();
            JSONArray json = new JSONArray(penjson);
            for (int j = 0; j < json.size(); j++) {
                JSONObject pj = json.getJSONObject(j);
                int jx = (int) Math.round(pj.getDouble("x") * 11.819955);
                int jy = (int) Math.round(pj.getDouble("y") * 11.819955);
                if (jx >= stx && jx <= endx && jy >= sty && jy <= endy) {
                    pj.set("x", pj.getDouble("x") - stx * 1.0 / 11.81995);
                    pj.set("y", pj.getDouble("y") - sty * 1.0 / 11.81995);
                    tmt1b.put(pj);
                }
            }
            penJsonV2.setTMT1B(tmt1b.toString());
            penJsonV2Mapper.updatePenJsonV2(penJsonV2);
        }else if(paperid==69150l) {
            //tmt2a
            int stx = 230;
            int sty = 600;
            int endx = 1830;
            int endy = 1500;
            JSONArray tmt2a = new JSONArray();
            JSONArray json = new JSONArray(penjson);
            for (int j = 0; j < json.size(); j++) {
                JSONObject pj = json.getJSONObject(j);
                int jx = (int) Math.round(pj.getDouble("x") * 11.819955);
                int jy = (int) Math.round(pj.getDouble("y") * 11.819955);
                if (jx >= stx && jx <= endx && jy >= sty && jy <= endy) {
                    pj.set("x", pj.getDouble("x") - stx * 1.0 / 11.81995);
                    pj.set("y", pj.getDouble("y") - sty * 1.0 / 11.81995);
                    tmt2a.put(pj);
                }
            }
            penJsonV2.setTMT2A(tmt2a.toString());
            penJsonV2Mapper.updatePenJsonV2(penJsonV2);
        }else if(paperid==69151l) {
            //tmt2b
            int stx = 60;
            int sty = 300;
            int endx = 2010;
            int endy = 28900;
            JSONArray tmt2b = new JSONArray();
            JSONArray json = new JSONArray(penjson);
            for (int j = 0; j < json.size(); j++) {
                JSONObject pj = json.getJSONObject(j);
                int jx = (int) Math.round(pj.getDouble("x") * 11.819955);
                int jy = (int) Math.round(pj.getDouble("y") * 11.819955);
                if (jx >= stx && jx <= endx && jy >= sty && jy <= endy) {
                    pj.set("x", pj.getDouble("x") - stx * 1.0 / 11.81995);
                    pj.set("y", pj.getDouble("y") - sty * 1.0 / 11.81995);
                    tmt2b.put(pj);
                }
            }
            penJsonV2.setTMT2B(tmt2b.toString());
            penJsonV2Mapper.updatePenJsonV2(penJsonV2);
        }else if(paperid==69164l) {
            //cfta
            int stx = 380;
            int sty = 1230;
            int endx = 1750;
            int endy = 2625;
            JSONArray cfta = new JSONArray();
            JSONArray json = new JSONArray(penjson);
            for (int j = 0; j < json.size(); j++) {
                JSONObject pj = json.getJSONObject(j);
                int jx = (int) Math.round(pj.getDouble("x") * 11.819955);
                int jy = (int) Math.round(pj.getDouble("y") * 11.819955);
                if (jx >= stx && jx <= endx && jy >= sty && jy <= endy) {
                    pj.set("x", pj.getDouble("x") - stx * 1.0 / 11.81995);
                    pj.set("y", pj.getDouble("y") - sty * 1.0 / 11.81995);
                    cfta.put(pj);
                }
            }
            penJsonV2.setCFTA(cfta.toString());
            penJsonV2Mapper.updatePenJsonV2(penJsonV2);
        }else if(paperid==69165l) {
            //cftb
            int stx = 360;
            int sty = 440;
            int endx = 1750;
            int endy = 1840;
            JSONArray cftb = new JSONArray();
            JSONArray json = new JSONArray(penjson);
            for (int j = 0; j < json.size(); j++) {
                JSONObject pj = json.getJSONObject(j);
                int jx = (int) Math.round(pj.getDouble("x") * 11.819955);
                int jy = (int) Math.round(pj.getDouble("y") * 11.819955);
                if (jx >= stx && jx <= endx && jy >= sty && jy <= endy) {
                    pj.set("x", pj.getDouble("x") - stx * 1.0 / 11.81995);
                    pj.set("y", pj.getDouble("y") - sty * 1.0 / 11.81995);
                    cftb.put(pj);
                }
            }
            penJsonV2.setCFTB(cftb.toString());
            penJsonV2Mapper.updatePenJsonV2(penJsonV2);
        }else if(paperid==69168l) {
            //cftc
            int stx = 360;
            int sty = 440;
            int endx = 1760;
            int endy = 1840;
            JSONArray cftc = new JSONArray();
            JSONArray json = new JSONArray(penjson);
            for (int j = 0; j < json.size(); j++) {
                JSONObject pj = json.getJSONObject(j);
                int jx = (int) Math.round(pj.getDouble("x") * 11.819955);
                int jy = (int) Math.round(pj.getDouble("y") * 11.819955);
                if (jx >= stx && jx <= endx && jy >= sty && jy <= endy) {
                    pj.set("x", pj.getDouble("x") - stx * 1.0 / 11.81995);
                    pj.set("y", pj.getDouble("y") - sty * 1.0 / 11.81995);
                    cftc.put(pj);
                }
            }
            penJsonV2.setCFTC(cftc.toString());
            penJsonV2Mapper.updatePenJsonV2(penJsonV2);
        }else if(paperid==69143l){
            //moca1
            int stx1 = 530;
            int sty1 = 620;
            int endx1  = 1530;
            int endy1 = 1420;
            //moca2
            int stx2 = 830;
            int sty2 = 1700;
            int endx2  = 1975;
            int endy2 = 2850;

            JSONArray moca1 = new JSONArray();
            JSONArray moca2 = new JSONArray();
            JSONArray json = new JSONArray(penjson);
            for (int j=0;j<json.size();j++){
                JSONObject pj = json.getJSONObject(j);
                int jx = (int) Math.round(pj.getDouble("x")*11.819955);
                int jy = (int) Math.round(pj.getDouble("y")*11.819955);

                if(jx>=stx1&&jx<=endx1&&jy>=sty1&&jy<=endy1){
                    pj.set("x",pj.getDouble("x")-stx1*1.0/11.81995);
                    pj.set("y",pj.getDouble("y")-sty1*1.0/11.81995);
                    moca1.put(pj);
                }
                if(jx>=stx2&&jx<=endx2&&jy>=sty2&&jy<=endy2){
                    pj.set("x",pj.getDouble("x")-stx2*1.0/11.81995);
                    pj.set("y",pj.getDouble("y")-sty2*1.0/11.81995);
                    moca2.put(pj);
                }

            }
            penJsonV2.setMOCA1(moca1.toString());
            penJsonV2.setMOCA2(moca2.toString());
            penJsonV2Mapper.updatePenJsonV2(penJsonV2);
        }else if(paperid==69144l){
            //moca3
            int stx3 = 250;
            int sty3 = 730;
            int endx3  = 1865;
            int endy3 = 2350;

            JSONArray moca3 = new JSONArray();
            JSONArray json = new JSONArray(penjson);
            for (int j=0;j<json.size();j++){
                JSONObject pj = json.getJSONObject(j);
                int jx = (int) Math.round(pj.getDouble("x")*11.819955);
                int jy = (int) Math.round(pj.getDouble("y")*11.819955);

                if(jx>=stx3&&jx<=endx3&&jy>=sty3&&jy<=endy3){
                    pj.set("x",pj.getDouble("x")-stx3*1.0/11.81995);
                    pj.set("y",pj.getDouble("y")-sty3*1.0/11.81995);
                    moca3.put(pj);
                }

            }
            penJsonV2.setMOCA3(moca3.toString());
            penJsonV2Mapper.updatePenJsonV2(penJsonV2);
        }
    }
}
